package com.itheima.leyou.service;

import com.itheima.leyou.dao.StorageDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class StorageService {

    @Resource
    StorageDao storageDao;

    public Map<String, Object> getStockStorage(String sku_id){
        //1、判断传入的参数是不是有误
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (sku_id==null||sku_id.equals("")){
            resultMap.put("result", false);
            resultMap.put("msg", "传入参数有误！");
            return resultMap;
        }

        //2、从storageDao里取数据
        ArrayList<Map<String, Object>> list = storageDao.getStockStorage(sku_id);
        if (list==null||list.isEmpty()){
            resultMap.put("result", false);
            resultMap.put("msg", "没有取出数据！");
            return resultMap;
        }

        //3、返回数据
        resultMap.put("storage", list);
        resultMap.put("result", true);
        resultMap.put("msg", "");
        return resultMap;
    }

    public Map<String, Object> insertStorage(String sku_id, double in_quanty, double out_quanty){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        //1、判断传入的参数是否有误
        if (sku_id==null||sku_id.equals("")){
            resultMap.put("result", false);
            resultMap.put("msg", "sku_id不能为空！");
            return resultMap;
        }

        if (in_quanty==0&&out_quanty==0){
            resultMap.put("result", false);
            resultMap.put("msg", "入的数量和出的数量不能同时为0！");
            return resultMap;
        }

        //2、从dao层取数据
        resultMap = storageDao.insertStorage(sku_id, in_quanty, out_quanty);

        //3、返回数据
        return resultMap;
    }
}
